/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package twitch_game;

// Imports
import org.jibble.pircbot.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

/**
 *
 * @author kgills
 */
public class MyBot extends PircBot{
    
    // Private variables
    Robot robot;
    Graphics myGraphics = new Graphics();
    
    // Constructor for this class
    public MyBot(String nick) throws AWTException  {
        this.setName(nick);
        
        // Create the robot object for the key presses
        try {
            robot = new Robot();
        } catch (AWTException e) {
            System.out.println("Error creating robot");
            System.exit(0);
        }
        

    }
    
    //Called when a message is sent to the channel
    @Override
    public void onMessage(String channel, String sender,
                       String login, String hostname, String message) {
        
        System.out.println(sender+":"+message);
        
        switch(message.toLowerCase()){
            case "up": cmdUp(sender); break;
            case "down": cmdDown(sender); break;
            case "left": cmdLeft(sender); break;
            case "right": cmdRight(sender); break;
            case "start": cmdStart(sender); break;
            case "select": cmdSelect(sender); break;
            case "a": cmdA(sender); break;
            case "b": cmdB(sender); break;
        }
    }
    
    // Private functions for when a command is given
    // Uses robot to press the key and updates the graphics object
    private void cmdLeft(String user){
        robot.keyPress(KeyEvent.VK_0);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_0);
        myGraphics.pushLabel(user+": "+"left");
    }
    
    private void cmdRight(String user){
        robot.keyPress(KeyEvent.VK_1);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_1);
        myGraphics.pushLabel(user+": "+"right");
    }
    
    private void cmdUp(String user){
        robot.keyPress(KeyEvent.VK_2);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_2);
        myGraphics.pushLabel(user+": "+"up");
    }
    
    private void cmdDown(String user){
        robot.keyPress(KeyEvent.VK_3);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_3);
        myGraphics.pushLabel(user+": "+"down");
    }
    
    private void cmdA(String user){
        robot.keyPress(KeyEvent.VK_4);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_4);
        myGraphics.pushLabel(user+": "+"A");
    }
    
    private void cmdB(String user){
        robot.keyPress(KeyEvent.VK_5);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_5);
        myGraphics.pushLabel(user+": "+"B");
    }
    
    private void cmdStart(String user){
        robot.keyPress(KeyEvent.VK_6);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_6);
        myGraphics.pushLabel(user+": "+"start");
    }
    
    private void cmdSelect(String user){
        robot.keyPress(KeyEvent.VK_7);
        robot.delay(50);
        robot.keyRelease(KeyEvent.VK_7);
        myGraphics.pushLabel(user+": "+"select");
    }
}
